﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Unipa.Framework.Data;
using dokuzeylulworkshop.MasterUtilities;
using DokuzEylul.Sinav.Entities;
using DokuzEylul.Sinav.Utility_Master;
using System.Collections;
using System.Collections.Generic;
using DokuzEylul.Sinav.Interfaces;

namespace dokuzeylulworkshop.BaseUtility
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);


            if (!(Page is ILogin))
            {
                if (m_CurrentUser.UserId  == Guid.Empty || m_CurrentUser == null)
                {
                    Response.Redirect("Login.aspx");
                }
                if (!yetkilimi(m_MenuID))
                {
                    throw new Exception("bu uygulama için yetkiniz yok");
                }
            }
        }
        /// <summary>
        /// verilenmenu ID ve role koduna göre ilgili yetkisinin
        /// olup olmadığını database de kontrol eder
        /// </summary>
        /// <param name="_menuID">ilgili menu Id si</param>
        /// <param name="RoleCode">Role kodu</param>
        /// <returns></returns>
        public bool yetkilimi(int _menuID, string RoleCode)
        {
            if (m_CurrentMenuUserRoles != null && m_CurrentMenuUserRoles.Count != 0)
                foreach (SinavUserRole _role in m_CurrentMenuUserRoles)
                {
                    if (_role.MenuId == _menuID)
                        if (_role.RoleCode == RoleCode)
                            return true;
                }
            return false;
        }
        /// <summary>
        /// ilgili menu Id si için tanımlı herhangi bir
        /// yetki varmı yokmu ona bakar
        /// </summary>
        /// <param name="_menuID">menu ID</param>
        /// <returns></returns>
        public bool yetkilimi(int _menuID)
        {
            if (_menuID == 0)
                return true;
            if (m_CurrentMenuUserRoles != null && m_CurrentMenuUserRoles.Count != 0)
                foreach (SinavUserRole _role in m_CurrentMenuUserRoles)
                {
                    if (_role.MenuId == -1)
                        return true;
                    if (_role.MenuId == _menuID)
                        return true;
                }
            return false;
        }
        public dbmaster m_dbmaster
        {
            get
            {
                dbmaster db = Session["DataBase_Context"] as dbmaster;
                if (db == null)
                    Session["DataBase_Context"] = db = dbmaster.Instance;
                return db;
            }
        }
        public SinavUser m_CurrentUser
        {
            get
            {
                SinavUser _result = Session["Current_User"] as SinavUser;
                if (_result == null)
                    Session["Current_User"] = _result = new SinavUser();
                return _result;
            }
            set
            {
                Session["Current_User"] = value;

            }
        }
        public IList<SinavUserRole> m_CurrentMenuUserRoles
        {
            get
            {
                IList<SinavUserRole> _result = Session["Current_User_Roles"] as IList<SinavUserRole>;
                if (_result == null)
                {
                    if (this.m_CurrentUser.UserId != Guid.Empty)
                        Session["Current_User_Role"] = _result = UserUtilities.Instance.getRoles(this.m_CurrentUser);
                    else
                        throw new Exception("Yetkisi Çağrılacak Bir Kullanıcı Bulunamadı");
                }
                return _result;
            }
            set
            {
                Session["Current_User_Roles"] = value;
            }

        }
        public IList<SinavUserRole> m_CurrentUygulamaUserRoles
        {
            get
            {
                IList<SinavUserRole> _result = Session["Uygulama_Current_Roles"] as IList<SinavUserRole>;
                if (_result == null)
                {
                    if (this.m_CurrentUser.UserId != Guid.Empty)
                        Session["Uygulama_Current_Roles"] = _result = UserUtilities.Instance.getUygulamaRoles(this.m_CurrentUser);
                    else
                        throw new Exception("Yetkisi çağırılacak Bir Kullanıcı Bulunamadı");
                }
                return _result;
            }
        }
        public void LoadUserRoles()
        {
            if (this.m_CurrentUser.UserId != Guid.Empty)
            {
                m_CurrentMenuUserRoles = UserUtilities.Instance.getRoles(this.m_CurrentUser);
            }
            else
                throw new Exception("Yetkileri istenecek personel bulunamadı!");
        }
        public bool AuthenticateUser(string uid, string pwd)
        {
            SinavUser _result = UserUtilities.Instance.authenticateUser(uid, pwd);
            if (_result != null)
            {
                this.m_CurrentUser = _result;
                return true;
            }
            else
                return false;
        }
        public virtual int m_MenuID
        {
            get
            {
                return 0;
            }
        }
        

    }
}
